package com.lagou.edu.interceptor;

import com.lagou.edu.constant.RequestConstant;
import com.lagou.edu.pojo.Account;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 权限拦截器
 *
 * @author srh
 * @date 2020/03/20
 **/
public class AccountHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        // 对登陆权限进行验证
        Account account = (Account) request.getSession(false).getAttribute(RequestConstant.TOKEN);
        if (null == account) {
            request.setAttribute(RequestConstant.ERROR_MSG, "无权限访问，请先登录");
            request.getRequestDispatcher(RequestConstant.LOGIN_VIEW).forward(request, response);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
